package org.cbg.panel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;

import org.cbg.bean.SearchGoodsResultBean;
import org.cbg.bean.SearchMyGoodsBean;
import org.cbg.btn.CBGMySaleBtn;
import org.cbg.entity.Salegoods;
import org.cbg.until.TraslationTableYijishangpinUntil;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.CutButtonImage;
import org.come.until.GsonUtil;

import com.tool.tcpimg.UIUtils;



/**
 * 我要卖 已寄售品
 * 
 * @author Administrator
 * 
 */

public class TraslationWantSentYijishangpinJpanel extends JLayeredPane {


    /** 下拉框,页码,上排序,下排序 */
    private JLabel chooseLeft, yema, upSort, downSort;
    /** 下拉框列表 */
    private TraslationSelectOptionJpanel chooseLeftModel;
    private JTable jTable;
    private JScrollPane jScrollPane;
    private JPanel jPanel;
    // 下拉,首页,末页 ,左箭头,右箭头
    private CBGMySaleBtn downBtn, shouBtn, moBtn, leftBtn, rightBtn;
    private List<TraslationWantSentYijishangpinModelJpanel> lists;

    public TraslationWantSentYijishangpinJpanel() {
        this.setPreferredSize(new Dimension(760, 460));
        this.setOpaque(false);
        this.setLayout(null);

        jScrollPane = new JScrollPane();
        jScrollPane.setOpaque(false);
        jScrollPane.setBounds(8, 63, 750, 372);
        jScrollPane.setBorder(null);
        jPanel = TraslationTableYijishangpinUntil.TableModel(jScrollPane);
        jScrollPane.getVerticalScrollBar().setUnitIncrement(50);

        /** 左下拉框设置 */
        chooseLeft = new JLabel("全部");
        chooseLeft.setBounds(17, 8, 100, 20);
        chooseLeft.setForeground(UIUtils.COLOR_cbg2);
        chooseLeft.setOpaque(false);
        this.add(chooseLeft);
        String[] rowDataLeft = { "全部", "未上架", "已上架", "被下单", "已卖出" };
        chooseLeftModel = new TraslationSelectOptionJpanel(100, 193, "inkImg/old/1/Z1000.png", rowDataLeft);
        chooseLeftModel.setBounds(15, 30, 100, 193);
        chooseLeftModel.setVisible(false);
        this.add(chooseLeftModel, 10, 1);
        chooseLeftModel.getJlist().addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                int count = chooseLeftModel.getJlist().getSelectedIndex();
                String getname = chooseLeftModel.getJlist().getSelectedValue();
                chooseLeft.setText(getname);
                chooseLeftModel.setVisible(false);
                yema.setText("1/1");
                search();
            }
        });
        this.add(jScrollPane, 10, -1);
        /** 页码设置 */
        yema = new JLabel("1/1");
        yema.setBounds(258+91, 442, 58, 17);
        yema.setForeground(UIUtils.COLOR_cbg8);
        yema.setOpaque(false);
        yema.setHorizontalAlignment(SwingConstants.CENTER);
        this.add(yema);

        downBtn = new CBGMySaleBtn("inkImg/number/b4/xlaj.png", 1, 0, this);
        downBtn.setBounds(99, 13, 13, 13);
        this.add(downBtn);
        shouBtn = new CBGMySaleBtn("inkImg/number/b4/fy1.png", 1, UIUtils.COLOR_BTNTEXT, UIUtils.TEXT_FONT, 1, "", this);
        shouBtn.setBounds(287, 442, 40, 20);
        this.add(shouBtn);
        moBtn = new CBGMySaleBtn("inkImg/number/b4/fy4.png", 1, UIUtils.COLOR_BTNTEXT, UIUtils.TEXT_FONT, 2, "", this);
        moBtn.setBounds(433, 442, 40, 20);
        this.add(moBtn);
        leftBtn = new CBGMySaleBtn("inkImg/number/b4/fy2.png", 1, 3, this);
        leftBtn.setBounds(326, 442, 19, 20);
        this.add(leftBtn);
        rightBtn = new CBGMySaleBtn("inkImg/number/b4/fy3.png", 1, 4, this);
        rightBtn.setBounds(411, 442, 19, 20);
        this.add(rightBtn);
        /** 排序 */
        upSort = new JLabel();
        upSort.setBounds(280, 38, 10, 18);
        upSort.setBorder(null);
        upSort.setBackground(null);
        upSort.setOpaque(false);
        upSort.setIcon(CutButtonImage.getImage("inkImg/old/1/Z1002.png", -1, -1));
        this.add(upSort);
        upSort.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                upSort.setIcon(CutButtonImage.getImage("inkImg/old/1/Z1003.png", 10, 18));
                downSort.setIcon(CutButtonImage.getImage("inkImg/old/1/Z1004.png", 10, 18));
            }
        });
        downSort = new JLabel();
        downSort.setBounds(296, 39, 10, 18);
        downSort.setBorder(null);
        downSort.setBackground(null);
        downSort.setOpaque(false);
        downSort.setIcon(CutButtonImage.getImage("inkImg/old/1/Z1004.png", 10, 18));
        this.add(downSort);
        downSort.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                upSort.setIcon(CutButtonImage.getImage("inkImg/old/1/Z1002.png", 10, 18));
                downSort.setIcon(CutButtonImage.getImage("inkImg/old/1/Z1005.png", 10, 18));
            }
        });
    }

    private ImageIcon icon;

    @Override
    protected void paintComponent(Graphics g) {
        // TODO Auto-generated method stub
        super.paintComponent(g);
        if (icon == null) {
            icon = new ImageIcon("inkImg/number/b4/wpxq.png");
        }
        g.drawImage(icon.getImage(), 0, 5, 760, 55, this);


    }

    public void xl() {
        chooseLeftModel.setVisible(!chooseLeftModel.isVisible());
    }

    public String YSqh(int type) {
        String[] vs = yema.getText().split("/");
        int dq = Integer.parseInt(vs[0]);
        int max = Integer.parseInt(vs[1]);
        if (type == 1) {
            type = 1;
        } else if (type == 2) {
            type = max;
        } else if (type == 3) {
            type = dq - 1;
        } else if (type == 4) {
            type = dq + 1;
        }
        if (type < 1) {
            return "已在首页";
        } else if (type > max) {
            return "已在末页";
        } else if (dq == type) {
            return "已在该页";
        }
        yema.setText(type + "/" + max);
        search();
        return null;
    }

    /** 搜索 */
    public void search() {
        String[] vs = yema.getText().split("/");
        int dq = Integer.parseInt(vs[0]);
        String typename = chooseLeft.getText();
        SearchMyGoodsBean searchMyGoodsBean = new SearchMyGoodsBean();
        searchMyGoodsBean.setPageNum(dq);
        if ("未上架".equals(typename)) {
            searchMyGoodsBean.setFlag(1);
        } else if ("已上架".equals(typename)) {
            searchMyGoodsBean.setFlag(2);
        } else if ("被下单".equals(typename)) {
            searchMyGoodsBean.setFlag(3);
        } else if ("已卖出".equals(typename)) {
            searchMyGoodsBean.setFlag(4);
        }
        String sendmes = Agreement.getAgreement().searchMyWaresAgreement(
                GsonUtil.getGsonUtil().getgson().toJson(searchMyGoodsBean));
        SendMessageUntil.toServer(sendmes);
    }

    /** 显示 */
    public void MyShow() {
        yema.setText("1/1");
        search();
    }

    /** 展示 */
    public void MyShow(SearchGoodsResultBean bean) {
        String[] vs = yema.getText().split("/");
        yema.setText(vs[0] + "/" + bean.getTotal());
        int size = getLists().size();
        List<Salegoods> salegoods = bean.getSalegoods();
        int p = 0;
        if (salegoods != null) {
            p = salegoods.size();
            for (int i = 0; i < p; i++) {
                xiugai(salegoods.get(i), i);
            }
        }
        for (int i = size - 1; i >= p; i--) {
            TraslationWantSentYijishangpinModelJpanel modelJpanel = getLists().remove(i);
            jPanel.remove(modelJpanel);
        }
        tz();
    }

    /** 修改 */
    public void xiugai(Salegoods salegoods, int p) {
        TraslationWantSentYijishangpinModelJpanel modelJpanel = null;
        if (p < getLists().size()) {
            modelJpanel = getLists().get(p);
        }
        if (modelJpanel != null) {
            modelJpanel.SX(salegoods);
        } else {
            modelJpanel = new TraslationWantSentYijishangpinModelJpanel(salegoods);
            getLists().add(modelJpanel);
            jPanel.add(modelJpanel);
        }
    }

    /** 移除 */
    public void removeJpanel(TraslationWantSentYijishangpinModelJpanel modelJpanel) {
        getLists().remove(modelJpanel);
        jPanel.remove(modelJpanel);
        tz();
    }

    /** 调整窗体位置 */
    public void tz() {
        for (int i = 0; i < lists.size(); i++) {
            TraslationWantSentYijishangpinModelJpanel modelJpanel = lists.get(i);
            modelJpanel.setBounds(0, i * 62, 740, 60);
        }
        jPanel.setPreferredSize(new Dimension(760, 62 * lists.size()));
        jScrollPane.getViewport().setViewSize(jPanel.getPreferredSize());
    }


    public JLabel getChooseLeft() {
        return chooseLeft;
    }

    public void setChooseLeft(JLabel chooseLeft) {
        this.chooseLeft = chooseLeft;
    }

    public JLabel getYema() {
        return yema;
    }

    public void setYema(JLabel yema) {
        this.yema = yema;
    }

    public JLabel getUpSort() {
        return upSort;
    }

    public void setUpSort(JLabel upSort) {
        this.upSort = upSort;
    }

    public JLabel getDownSort() {
        return downSort;
    }

    public void setDownSort(JLabel downSort) {
        this.downSort = downSort;
    }

    public TraslationSelectOptionJpanel getChooseLeftModel() {
        return chooseLeftModel;
    }

    public void setChooseLeftModel(TraslationSelectOptionJpanel chooseLeftModel) {
        this.chooseLeftModel = chooseLeftModel;
    }

    public JTable getjTable() {
        return jTable;
    }

    public void setjTable(JTable jTable) {
        this.jTable = jTable;
    }

    public JScrollPane getjScrollPane() {
        return jScrollPane;
    }

    public void setjScrollPane(JScrollPane jScrollPane) {
        this.jScrollPane = jScrollPane;
    }

    public List<TraslationWantSentYijishangpinModelJpanel> getLists() {
        if (lists == null) {
            lists = new ArrayList<>();
        }
        return lists;
    }

}
